package com.project8.main;

import java.io.Serializable;

import android.app.Dialog;
import android.graphics.Color;
import android.text.Editable;
import android.text.Selection;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NotebookTabTouch implements Serializable{

	Main main;
	
	public NotebookTabTouch(Main mainInstance)
	{
		main = mainInstance;
	}
	
	public void initializeTabSettings(TextView nbTab)
	{
		final TextView nbTab2 = nbTab;
		
		nbTab.setOnTouchListener(new OnTouchListener()
		{
			public boolean onTouch(View view, MotionEvent event)
			{
				main.currentTab = (TextView)view;
				
				if(event.getAction() == MotionEvent.ACTION_DOWN)
				{
					main.downTime = System.currentTimeMillis();
					main.firstPress = true;
					 
					for(int tab = 0; tab < main.allNBTabs.size(); tab++)
	    			{
	    				if(main.allNBTabs.get(tab).equals(view))
	    				{
	    					((TextView)view).setBackgroundColor(Color.parseColor("#FFCC0000"));
	    					((TextView)view).setTextColor(Color.parseColor("#FFFFFFFF"));
	    					((TextView)view).setTextSize(18);

	    					System.out.println("Tab " + tab);
	    					
	    					main.currentNBPage.setTextOnPage(main.notesContent.getText());
	    					main.currentNBPage = main.theNotebook.getNBPage(tab);
	    					main.currentTabNum = tab;
	      				}
	    				else
	    				{
	    					main.allNBTabs.get(tab).setBackgroundColor(Color.parseColor("#FF980000"));
	    					main.allNBTabs.get(tab).setTextColor(Color.parseColor("#FFCCCCCC"));
	    					main.allNBTabs.get(tab).setTextSize(14);
	    				}
	    			}
					
					/* Set appropriate content in Notebook, and place cursor at end of last note. */
					main.notesContent.setText(main.currentNBPage.getTextOnPage());
					Editable notes = main.notesContent.getText(); 
					int position = notes.length(); 
					Selection.setSelection(notes, position); 
					
					return true;
				}
				if(event.getAction() == MotionEvent.ACTION_MOVE)
				{
					double holdTime = System.currentTimeMillis();
					
					if((holdTime - main.downTime) > 700 && main.firstPress == true)
					{
						final Dialog deleteTab = new Dialog(main);
						LinearLayout deleteTabLayout = new LinearLayout(main);
						final TextView deleteLabel = new TextView(main);
						final EditText changeName = new EditText(main);
						LinearLayout changeName2 = new LinearLayout(main);
							
						LinearLayout buttonLayout = new LinearLayout(main);
						Button okBut = new Button(main);
						Button canBut = new Button(main);
						Button changeBut = new Button(main);
						
						changeName2.setWeightSum(1);
						buttonLayout.setWeightSum(1);
						deleteTabLayout.setWeightSum(1);
						deleteTabLayout.setOrientation(main.VERTICAL);
						buttonLayout.setOrientation(main.HORIZONTAL);
						changeBut.setText("Change Label");
						changeName.setSingleLine(true);
						changeName.setMinimumWidth(500);
						changeName2.addView(changeName);
						changeName2.setPadding(5, 5, 5, 5);
						changeName.setText(((TextView)view).getText());
						deleteLabel.setPadding(113, 0, 0, 25);
						deleteLabel.setTextColor(Color.parseColor("White"));
						deleteLabel.setTextSize(18);
						buttonLayout.addView(changeBut);
						buttonLayout.addView(okBut);
						buttonLayout.addView(canBut);
						buttonLayout.setMinimumWidth(300);
						buttonLayout.setPadding(80, 0, 50, 20);
						deleteTabLayout.addView(changeName2);
						deleteTabLayout.addView(buttonLayout);
						
						okBut.setText("Erase Page");
						okBut.setMinWidth(100);
						okBut.setTextColor(Color.parseColor("Black"));
												
						canBut.setText("Cancel");
						canBut.setMinWidth(100);
						canBut.setTextColor(Color.parseColor("Black"));
						deleteTab.setContentView(deleteTabLayout);
						deleteTab.show();
						
						
						okBut.setOnClickListener(new View.OnClickListener()
						{
							public void onClick(View v)
							{
								for(int t = 0; t < main.allNBTabs.size(); t++)
								{
									if(main.currentTab.equals(main.allNBTabs.get(t)))
									{											
										int nextTab = t;
										if(t == main.allNBTabs.size()-1)
										{
											if(main.allNBTabs.size() == 1)
											{
												main.currentTab = main.allNBTabs.get(t);
												main.currentTabNum = t;
												nextTab = t;
											}
											else
											{
												main.currentTab = main.allNBTabs.get(t-1);
												main.currentTabNum = t;
												nextTab = t-1;
											}
										}
										else
										{
											main.currentTab = main.allNBTabs.get(t+1);
											main.currentTabNum = t;
											nextTab = t+1;
										}
										
				    					main.currentTab.setBackgroundColor(Color.parseColor("#FFCC0000"));
				    					main.currentTab.setTextColor(Color.parseColor("#FFFFFFFF"));
				    					main.currentTab.setTextSize(18);
				    					
				    					main.currentNBPage = main.theNotebook.getNBPage(nextTab);
				    					
										if(main.theNotebook.getNumNotebookPages() > 1)
										{
											main.theNotebook.removeNBPage(t);
											main.allNBTabs.remove(t);
											main.tabLayout2.removeView(nbTab2);
											main.numberOfTabs--;
										}
										else
										{
											nextTab = t;
											main.theNotebook.getNBPage(t).resetAttributes();
											main.allNBTabs.get(t).setText("Notes");
											
											System.out.println("Number of Tabs = " + main.theNotebook.getNumNotebookPages());
											System.out.println("Number of Tabs = " + main.allNBTabs.size());
										}
										
										/* Set appropriate content in Notebook, and place cursor at end of last note. */
				    					main.notesContent.setText(main.currentNBPage.getTextOnPage());
				    					Editable notes = main.notesContent.getText(); 
				    					int position = notes.length(); 
				    					Selection.setSelection(notes, position);
									}
								}
								
								deleteTab.cancel();		
							}
						});
						
						canBut.setOnClickListener(new View.OnClickListener()
						{
							public void onClick(View v)
							{
								deleteTab.cancel();
							}
						});
						
						changeBut.setOnClickListener(new View.OnClickListener()
						{
							public void onClick(View v)
							{
								nbTab2.setText(changeName.getText().toString());
								main.currentNBPage.setTabLabel(nbTab2.getText().toString());
								//for(int tab = 0; tab < main.allNBTabs.size(); tab++)
								//{
								//	if(v.equals(main.allNBTabs.get(tab)))
								//		main.theNotebook.getNBPage(tab).setTabLabel(changeName.getText().toString());
								//}
								deleteTab.cancel();
							}
						});

						
						main.firstPress = false;
						return true;
					}
				}
				return true;
			}
		});
	}
}
